package com.hskn.hss.module.tlworktype.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlcartype.entity.TlCarType;
import com.hskn.hss.module.tlworktype.entity.TlWorkType;
import com.hskn.hss.module.tlworktype.mapper.TlWorkTypeMapper;
import com.hskn.hss.module.tlworktype.service.ITlWorkTypeService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.GeometryUtil;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.redis.RedisUtil;
import com.hskn.hss.utils.redis.key.CarTypeKey;
import com.hskn.hss.utils.redis.key.WorkTypeKey;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-15
 */
@Service
public class TlWorkTypeServiceImpl extends ServiceImpl<TlWorkTypeMapper, TlWorkType> implements ITlWorkTypeService {

    @Resource
    TlWorkTypeMapper tlWorkTypeMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlWorkType tlWorkType = params.getObject("data", TlWorkType.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlWorkType.setCreateby(uid);
        tlWorkType.setCreatetime(new Date());
        tlWorkType.setDeptid(deptid);
        if (tlWorkType.getStatus() == null || tlWorkType.getStatus().isEmpty()) {
            tlWorkType.setStatus("1");
        }
        tlWorkTypeMapper.insert(tlWorkType);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlWorkType tlWorkType = params.getObject("data", TlWorkType.class);
        tlWorkType.setUpdateby(uid);
        tlWorkType.setUpdatetime(new Date());
        tlWorkTypeMapper.updateById(tlWorkType);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlWorkTypeMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlWorkType getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlWorkType tlWorkType = tlWorkTypeMapper.selectById(id);
        return tlWorkType;
    }

    @Override
    public List getSelectList(JSONObject params) {
        String status = params == null ? "" : params.getString("status");
        Map map = new HashMap();
        map.put("status", status);
        List resultList = tlWorkTypeMapper.getSelectList(map);
        return resultList;
    }


    @Override
    public TlWorkType getFromRedis(String workTypeId) {
        TlWorkType tlWorkType = null;
        if (StringUtils.isEmpty(workTypeId)) {
            return null;
        }
        try {
            tlWorkType = RedisUtil.get(WorkTypeKey.token, workTypeId, TlWorkType.class);
            if (null == tlWorkType) {
                tlWorkType = tlWorkTypeMapper.selectById(workTypeId);
                RedisUtil.set(WorkTypeKey.token, workTypeId, tlWorkType);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return tlWorkType;
    }

    @Override
    @DataScope(deptAlias = "deptid", userAlias = "createby, updateby")
    public JSONObject getList(BaseEntity entity, JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("worktype", params.getString("worktype"));
        map.put("isdriver", params.getString("isdriver"));
        map.put("status", params.getString("status"));
        map.put("params", entity.getParams());

        Page<TlWorkType> listPage = new Page<>(page, size, true);
        IPage<TlWorkType> iPage = tlWorkTypeMapper.listTlWorkType(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("worktypelist", iPage.getRecords());
        return json;
    }
    @Override
    public Map<String,TlWorkType> getTlWorkTypeMap(QueryWrapper<TlWorkType> workTypeQueryWrapper){
        Map<String,TlWorkType> workTypeMap = new HashMap<>();
        List<TlWorkType> workTypeList = tlWorkTypeMapper.selectList(workTypeQueryWrapper);
        if (CollectionUtils.isNotEmpty(workTypeList)){
            workTypeMap= GroupingByUtils.getGroupingForMapEntity(workTypeList,TlWorkType::getId,Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        return workTypeMap;
    }
}
